﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

/*
create table News_Type
(
newType nvarchar(20) primary key ,-- 新闻类别(主键)
newIntro nvarchar(100),-- 介绍
)

*/

public partial class Admin_NewsType_manager : System.Web.UI.Page
{
    /// <summary>页面加载事件
    /// 	<remark>2010-10-23 河北大学信管团队-水亚亚 </remark>
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        //检查用户权限
        if (Session["Uid"] == null)
        {
            Response.Redirect("AD_Login.aspx");
            return;
        }
        //情况消息框
        lblMessage.Text = "";
        if (!IsPostBack)
        {
            addOrChange();
            CancelNewNewsType.Text = "清空";
            //为GridView绑定数据
            Bind_GV();
        }
    }

    /// <summary>为控件绑定数据——GridView
    /// 	<remark>2010-10-23 河北大学信管团队-水亚亚 </remark>
    /// </summary>
    private void Bind_GV()
    {
     
        //定义sql语句
        string sql = "select newsType,newIntro from News_Type order by newsType desc";
        //取得数据并绑定到GridView
        GV.DataSource = DataAccess.GetDataTable(sql, null);
        //为GridView绑定主键
        GV.DataKeyNames = new string[] { "newsType" };
        //为GridView绑定数据
        GV.DataBind();
    }

    /// <summary>响应GridView事件——创建数据行时
    /// 	<remark>2010-10-23 河北大学信管团队-水亚亚 </remark>
    /// </summary>
    protected void GV_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {//如果是数据行
            //为数据行增加鼠标移动到行特性
            e.Row.Attributes.Add("onmouseover", "c=style.backgroundColor;this.style.backgroundColor='#6699ff'");
            //为数据行增加鼠标离开行特性
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        }
    }

    /// <summary>响应GridView事件——切换页码时
    /// 	<remark>2010-5-24 BY 水亚亚 </remark>
    /// </summary>
    protected void GV_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //设定GridView的页码为当前页码
        GV.PageIndex = e.NewPageIndex;
        //重新绑定数据
        Bind_GV();
    }

    /// <summary>响应GridView事件——删除记录时
    /// 	<remark>2010-10-23 河北大学信管团队-水亚亚 </remark>
    /// </summary>
    protected void GV_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sql = "delete from News_Type where newsType=@newsType";
        //定义参数
        SqlParameter[] para = { new SqlParameter("@newsType", SqlDbType.NVarChar) };
        //参数赋值
        para[0].Value = GV.DataKeys[e.RowIndex][0].ToString();
        //删除数据
        if (DataAccess.GetCount(sql, para) > 0)
        {//如果删除成功
            lblMessage.Text = "删除数据成功";
            //重新绑定数据
            Bind_GV();
        }
        else
        {//如果删除失败
            lblMessage.Text = "删除数据失败";
        }
    }
    protected void CreateNewNewsType_Click(object sender, EventArgs e)
    {
        string sql;
        if (addOrChange())
        {
            sql = "insert into News_Type(newsType,newIntro)values(@newsType,@newIntro)";
        }
        else
        {
            sql = "update News_Type set newIntro=@newIntro where newsType=@newsType";
        }
        SqlParameter[] para = new SqlParameter []{
            new SqlParameter("@newsType",txtNewsTypeNew.Text.Trim()),
            new SqlParameter("@newIntro",txtNewsIntroNew.Text)
        };
       

        if (DataAccess.ExecuteSQL(sql, para))
        {
            lblMessage.Text = addOrChange() ? "添加成功" : "修改成功";
            clear();
            this.txtNewsTypeNew.Enabled = true;
            Bind_GV();
   
        }
        else lblMessage.Text = addOrChange() ? "添加失败" : "修改失败";
         
    }
    private void clear()
    {
        this.txtNewsIntroNew.Text = "";
        this.txtNewsTypeNew.Text = ""; 
    }
    private bool addOrChange()
    {
        if (txtNewsTypeNew.Enabled==false)
        {
         
            return false;
        }
        return true;
    }
   
 
    protected void GV_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        CancelNewNewsType.Text = "取消";
        GridViewRow row = GV.Rows[e.NewSelectedIndex];
   
    }
    protected void GV_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow dr = GV.SelectedRow;
        txtNewsIntroNew.Text = dr.Cells[1].Text;
        txtNewsTypeNew.Enabled = false;
        txtNewsTypeNew.Text = dr.Cells[0].Text;
        lblMessage.Text = "请修改数据";

    }
    protected void CancelNewNewsType_Click(object sender, EventArgs e)
    {
        clear();
        this.txtNewsTypeNew.Enabled = true;
        CancelNewNewsType.Text = "清空";
    }
}